<template>
    <div class="seting-box">
        <van-nav-bar title="账号管理" fixed left-text="返回" left-arrow @click-left="goBack">
            <template #right>
                <van-icon name="fire-o" color="#ee0a24" size="20"/>
            </template>
        </van-nav-bar>
        <div class="input-item">
            <van-field v-model="nickName" label="昵称" />
            <van-field v-model="introduceSign" label="个性签名" />
            <van-field v-model="password" type='password' label="修改密码" />
        </div>
        <van-button class="save-btn" color="#1baeae" type="primary" @click="save" block>保存</van-button>
        <van-button class="save-btn" color="#1baeae" type="primary" @click="logout" block>退出登录</van-button>
    </div>
</template>

<script>
    import sHeader from '../../components/SimpleHeader'
    import { getCurrent, EditUserInfo, logout } from '../../api/user'
    import { setLocal } from '../../common/js/utils'
    import { Toast } from 'vant'

    export default {
        name: "userSetting",
        components: {
            sHeader
        },
        data() {
            return {
                nickName: '',
                introduceSign: '',
                password: ''
            }
        },
        async mounted() {
            const { data } = await getCurrent()
            this.nickName = data.nickname
            this.introduceSign = data.personalizedSignature
        },
        methods: {
            async save() {
                const params = {
                    introduceSign: this.introduceSign,
                    nickName: this.nickName,
                    passwordMd5: this.$md5(this.password)
                }
                const { data } = await EditUserInfo(params)
                Toast.success('保存成功')
            },
            async logout() {
                this.$store.dispatch('LoGout').then(() => {
                    window.location.href = '/'
                })
                // logout()
                // if (resultCode == 200) {
                //     setLocal('token', '')
                //     window.location.href = '/'
                // }
            },
            goBack(){
                this.$router.go(-1);
            }
        }
    }
</script>

<style lang="less" scoped>
    .seting-box {
        .input-item {
            margin-top: 44px;
        }
        .save-btn {
            width: 80%;
            margin: 20px auto ;
        }
    }
</style>
